<!-- templates/upload.html -->
<!DOCTYPE html>
<html>
<head>
    <title>大文件上传</title>
    <script>
        async function uploadFile() {
            const file = document.getElementById('fileInput').files[0];
            if (!file) return alert('请选择文件');
            
            // 获取URL中的密钥
            const urlParams = new URLSearchParams(window.location.search);
            const apiKey = urlParams.get('key');
            
            // 生成文件唯一ID
            const fileId = uuidv4();
            const chunkSize = 10 * 1024 * 1024; // 5MB分块
            const totalChunks = Math.ceil(file.size / chunkSize);
            
            // 分块上传
            for (let i = 0; i < totalChunks; i++) {
                const start = i * chunkSize;
                const end = Math.min(start + chunkSize, file.size);
                const chunk = file.slice(start, end);
                
                const formData = new FormData();
                formData.append('chunk', chunk);
                formData.append('chunkIndex', i);
                formData.append('totalChunks', totalChunks);
                formData.append('fileId', fileId);
                formData.append('filename', file.name);
                
                const response = await fetch('/python-file-server/upload_chunk', {
                    method: 'POST',
                    headers: { 'X-API-Key': apiKey },
                    body: formData
                });
                
                if (!response.ok) {
                    throw new Error(`块 ${i+1}/${totalChunks} 上传失败`);
                }
                updateProgress(i + 1, totalChunks);
            }
            
            // 合并文件
            const mergeResponse = await fetch('/python-file-server/merge', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'X-API-Key': apiKey
                },
                body: new URLSearchParams({
                    fileId: fileId,
                    totalChunks: totalChunks,
                    filename: file.name
                })
            });
            
            const result = await mergeResponse.json();
            alert(`文件上传成功！大小: ${(result.size / 1024 / 1024).toFixed(2)}MB`);
        }
        
        function updateProgress(current, total) {
            const percent = Math.round((current / total) * 100);
            document.getElementById('progress').innerText = `上传进度: ${percent}%`;
        }
        
        function uuidv4() {
            return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                const r = Math.random() * 16 | 0;
                const v = c === 'x' ? r : (r & 0x3 | 0x8);
                return v.toString(16);
            });
        }
    </script>
</head>
<body>
    <h1>大文件上传 (最大5GB)</h1>
    <input type="file" id="fileInput">
    <button onclick="uploadFile()">开始上传</button>
    <p id="progress"></p>
</body>
</html>